﻿/****************************************************************************
*                                                                           *
* Copyright (c) Ionescu M. Marius. All rights reserved(2010).               *
*                                                                           *
****************************************************************************/

using System;
using System.Data;
using System.Security.Principal;

public partial class Tools_Download : System.Web.UI.Page
{
	protected void Page_Load(object sender, EventArgs e)
	{
		DownloadFile();
	}

	private DataSet GetSystemConfigDataSet()
	{
		DataSet ds_SystemConfig = (DataSet)Session["ds_SystemConfig"];
		if (ds_SystemConfig == null)
		{
            ds_SystemConfig = Hipocrate.DB.DBConnection.GetData("System_Config_Select", new string[] { }, new object[] { });
			Session["ds_SystemConfig"] = ds_SystemConfig;
		}
		return ds_SystemConfig;
	}

	protected void DownloadFile()
	{
		bool bUseImpersonation = false;
		WindowsImpersonationContext impContext = null;
		string filepath = "";

		try
		{
			DataSet ds_SystemConfig = GetSystemConfigDataSet();
			DataView dv_ = new DataView(ds_SystemConfig.Tables[0]);
			switch (Request.QueryString["ID"])
			{
				case "-1":
					break;
				case "-2":
					break;
				case "-3":
					break;
				default:
					dv_.RowFilter = "Code = 'UploadRootPath'";

					if (dv_[0]["Name"].ToString().StartsWith("~"))
						bUseImpersonation = false;
					else
						bUseImpersonation = true;

					if (bUseImpersonation)
						impContext = ImpersonationHelper.StartImpersonate();

                    DataSet ds_DownloadFile = Hipocrate.DB.DBConnection.GetData("Download_Select", new string[] { "ID" }, new object[] { Request.QueryString["ID"] });
					DataView dv = ds_DownloadFile.Tables[0].DefaultView;
					if (dv.Count > 0)
						filepath = dv[0]["FileDownload"].ToString();
					break;
			}

			if (filepath != "")
			{
				System.IO.FileInfo file = new System.IO.FileInfo(filepath);
				if (file.Exists)
				{
					Response.ClearContent();
					Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
					Response.AddHeader("Content-Length", file.Length.ToString());
					Response.ContentType = ReturnExtension(file.Extension.ToLower());

					Response.TransmitFile(file.FullName);

					Response.End();
				}
				else
				{
					throw new Exception("Fisierul nu exista pe server!");
				}
			}
		}
		catch (System.Threading.ThreadAbortException) {}
		catch (Exception ex)
		{
			Response.Write(String.Format("<b style='color: Red'>Eroare: {0}</b>", ex.Message));
		}
	}

	private string ReturnExtension(string fileExtension)
	{
		switch (fileExtension)
		{
			case ".htm":
			case ".html":
			case ".log":
				return "text/HTML";
			case ".txt":
				return "text/plain";
			case ".doc":
				return "application/ms-word";
			case ".tiff":
			case ".tif":
				return "image/tiff";
			case ".asf":
				return "video/x-ms-asf";
			case ".avi":
				return "video/avi";
			case ".zip":
				return "application/zip";
			case ".xls":
			case ".csv":
				return "application/vnd.ms-excel";
			case ".gif":
				return "image/gif";
			case ".jpg":
			case "jpeg":
				return "image/jpeg";
			case ".bmp":
				return "image/bmp";
			case ".wav":
				return "audio/wav";
			case ".mp3":
				return "audio/mpeg3";
			case ".mpg":
			case "mpeg":
				return "video/mpeg";
			case ".rtf":
				return "application/rtf";
			case ".asp":
				return "text/asp";
			case ".pdf":
				return "application/pdf";
			case ".fdf":
				return "application/vnd.fdf";
			case ".ppt":
				return "application/mspowerpoint";
			case ".dwg":
				return "image/vnd.dwg";
			case ".msg":
				return "application/msoutlook";
			case ".xml":
			case ".sdxl":
				return "application/xml";
			case ".xdp":
				return "application/vnd.adobe.xdp+xml";
			default:
				return "application/octet-stream";
		}
	}
}
